package com.wyw.leetcode.learning.simple;

import java.util.HashMap;
import java.util.Map;

/**
 * leetcode topic 205
 * 同构字符串
 *
 * @Author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class Topic205 {
    public static void main(String[] args) {
        String s = "efg";
        String t = "app";
        System.out.println(isIsomorphic(s, t));
    }

    public static boolean isIsomorphic(String s, String t) {
        int length = s.length();
        HashMap<Character, Character> map = new HashMap<>(length);
        for(int i=0; i<length; i++) {
            if(!map.containsKey(s.charAt(i))) {
                if(map.containsValue(t.charAt(i))) {
                    return false;
                }
                map.put(s.charAt(i), t.charAt(i));
            } else {
                if(map.get(s.charAt(i))!=t.charAt(i)) {
                    return false;
                }
            }
        }

        return true;
    }

    public static boolean isIsomorphic1(String s, String t) {
        Map<Character, Character> s2t = new HashMap<Character, Character>();
        Map<Character, Character> t2s = new HashMap<Character, Character>();
        int len = s.length();
        for (int i = 0; i < len; ++i) {
            char x = s.charAt(i), y = t.charAt(i);
            if ((s2t.containsKey(x) && s2t.get(x) != y) ||
                    (t2s.containsKey(y) && t2s.get(y) != x)) {
                return false;
            }
            s2t.put(x, y);
            t2s.put(y, x);
        }
        return true;
    }
}
